package com.xxja.order.goods.shop.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxja.order.goods.dish.mapper.GoodsMapper;
import com.xxja.order.goods.dish.pojo.Goods;
import com.xxja.order.goods.shop.mapper.ShopMapper;
import com.xxja.order.goods.shop.pojo.Shop;
import com.xxja.order.goods.shop.pojo.ShopAndGoods;
import com.xxja.order.utils.exception.ExceptionCast;
import com.xxja.order.utils.exception.ExceptionCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    /**
     * 查询全部店铺信息
     * @return 店铺信息
     */
    public IPage<Shop> findAllShop(Page page) {
        Page<Shop> record = shopMapper.selectPage(page, null);
        if (record.getRecords() == null) {
            ExceptionCast.cast(ExceptionCode.SHOP_NONE);
        }
        return record;
    }


    public ShopAndGoods findShopAndGoods(Long id) {
        if (id == null) {
            ExceptionCast.cast(ExceptionCode.SHOP_ID_NONE);
        }
        Shop shop = this.shopMapper.selectById(id);
        // 不校验店铺信息存在否
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("shop_id", id);
        List<Goods> goods = this.goodsMapper.selectList(queryWrapper);
        ShopAndGoods shopAndGoods = new ShopAndGoods();
        shopAndGoods.setShop(shop);
        shopAndGoods.setGoods(goods);
        return shopAndGoods;
    }
}
